package com.example.demo.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.entity.Employee;
import com.example.demo.mapper.EmployeeMapper;
import com.example.demo.service.EmployeeService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.testng.annotations.Test;

import java.util.List;


@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Resource
    EmployeeService employeeService;

    //新增
    @PostMapping
    public Result<?> save(@RequestBody Employee employee){
        if(null != employeeService.selectByUsername(employee.getUsername())){
            return Result.error("-1","用户名已存在,请重新输入");
        }else{
            //默认密码123456
            if (!StrUtil.isNotBlank(employee.getPassword())) {
                employee.setPassword("123456");
            }
            if(!StrUtil.isNotBlank(employee.getNickName())){
                employee.setNickName(employee.getUsername());
            }
            employee.setRole("员工");
            employeeService.insert(employee);
            return Result.success();
        }
    }


    //修改
    @PutMapping
    public Result<?> update(@RequestBody Employee employee){
        employeeService.updateById(employee);
        return Result.success();
    }

    //删除
    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable Long id){
        employeeService.deleteById(id);
        return Result.success();
    }

    //分页查询
    @GetMapping("/page")
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "3") Integer pageSize,
                              @RequestParam(defaultValue = "") String search){

        Page<Employee> userPage = employeeService.selectPage(pageNum,pageSize,search);
        return Result.success(userPage);

    }

    //修改密码
    @PutMapping("/updatePassword")
    public Result<?> updatePassowrd(@RequestBody Employee employee){
        employeeService.updatePassowrd(employee);
            return Result.success();

    }
    @GetMapping("/getPassword")
    public Result<?> getPassword(@RequestParam int id, @RequestParam String password){
        int a=employeeService.getPassowrd(id,password);
        return Result.success(a);
    }
}
